package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.enums.EquipmentEnums;
import com.mxpio.erp.common.equipment.entity.ProjectManagement;
import com.mxpio.erp.common.equipment.entity.ProjectStatusRecord;
import com.mxpio.erp.common.equipment.service.ProjectManagementService;
import com.mxpio.erp.common.equipment.service.ProjectStatusRecordService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.security.entity.User;
import com.mxpioframework.security.util.SecurityUtils;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ProjectManagementServiceImpl extends BaseServiceImpl<ProjectManagement> implements ProjectManagementService {

    @Autowired
    private ProjectStatusRecordService projectStatusRecordService;

    private void insertStatusRecord(String proCode, String operate, String reason) {
        ProjectStatusRecord projectStatusRecord = new ProjectStatusRecord();//对状态记录进行赋值
        //设置proCode
        projectStatusRecord.setProCode(proCode);
        //设置操作节点(状态)
        projectStatusRecord.setOperate(operate);
        //设置操作人
        User loginUser = SecurityUtils.getLoginUser();
        projectStatusRecord.setOperator(loginUser.getNickname() + "/" + loginUser.getUsername());
        //设置操作时间
        projectStatusRecord.setOperateTime(new Date());
        //设置原因
        projectStatusRecord.setReason(reason);
        //保存
        projectStatusRecordService.save(projectStatusRecord);
    }

    @Override
    @Transactional
    public Result<ProjectManagement> updateProStatus(ProjectManagement projectManagement, EquipmentEnums.Project improgress) {
        projectManagement.setProStatus(improgress.getCode());
        this.update(projectManagement);
        // 状态
        projectManagement.setProStatus(EquipmentEnums.Project.LSSUED.getCode());
        // 用户
        projectManagement.setProUserId(projectManagement.getProUserId());
        // 时间
        projectManagement.setRealyStartTime(projectManagement.getRealyStartTime());
        // 原因
        projectManagement.setProStatus(EquipmentEnums.Project.LSSUED.getCode());
        insertStatusRecord(projectManagement.getProCode(), projectManagement.getProStatus(), projectManagement.getProStatus());
        return Result.OK();
    }

    @Override
    @Transactional
    public Result<ProjectManagement> updateTime(ProjectManagement projectManagement, EquipmentEnums.Project improgress) {
        projectManagement.setProStatus(improgress.getCode());
        projectManagement.setRealyStartTime(new Date());
        this.update(projectManagement);
        // 状态
        projectManagement.setProStatus(EquipmentEnums.Project.IMPROGRESS.getCode());
        // 用户
        projectManagement.setProUserId(projectManagement.getProUserId());
        // 时间
        projectManagement.setRealyStartTime(projectManagement.getRealyStartTime());
        // 原因
        projectManagement.setProStatus(EquipmentEnums.Project.IMPROGRESS.getCode());
        insertStatusRecord(projectManagement.getProCode(), projectManagement.getProStatus(), projectManagement.getProStatus());
        return Result.OK();
    }

    @Override
    @Transactional
    public void listExecute(ProjectManagement pm) {
        ProjectManagement selectPm = this.getById(ProjectManagement.class, pm.getProCode());
        // 状态
        selectPm.setProStatus(EquipmentEnums.Project.PLAN.getCode());
        // 用户
        selectPm.setProUserId(pm.getProUserId());
        // 时间
        selectPm.setRealyStartTime(pm.getRealyStartTime());
        // 原因
        selectPm.setProStatus(EquipmentEnums.Project.PLAN.getCode());
        this.update(selectPm);
        insertStatusRecord(pm.getProCode(), pm.getProStatus(), pm.getProStatus());
    }

    @Override
    @Transactional
    public Result<ProjectManagement> updateRealyEndTime(ProjectManagement projectManagement, EquipmentEnums.Project improgress) {
        projectManagement.setProStatus(improgress.getCode());
        projectManagement.setRealyEndTime(new Date());
        this.update(projectManagement);
        // 状态
        projectManagement.setProStatus(EquipmentEnums.Project.COMPLETED.getCode());
        // 用户
        projectManagement.setProUserId(projectManagement.getProUserId());
        // 时间
        projectManagement.setRealyStartTime(projectManagement.getRealyStartTime());
        // 原因
        projectManagement.setProStatus(EquipmentEnums.Project.COMPLETED.getCode());
        insertStatusRecord(projectManagement.getProCode(), projectManagement.getProStatus(), projectManagement.getProStatus());
        return Result.OK();
    }
}
